﻿#include "QCMultiMenu.h"


QCMultiMenu::QCMultiMenu(QWidget *parent) :
    QWidget(parent)
{
    m_bHorizon = true;
    m_bShowBorder = true;
    m_nMenuWidth = 44;
    m_nMenuHeight = 44;
}

QCMultiMenu::~QCMultiMenu()
{

}

void QCMultiMenu::paintEvent(QPaintEvent *)
{

}

void QCMultiMenu::mouseMoveEvent(QMouseEvent *event)
{
    int n = 0;
    ++n;
    event->ignore();
}

void QCMultiMenu::mousePressEvent(QMouseEvent *event)
{
    event->ignore();
}

void QCMultiMenu::mouseReleaseEvent(QMouseEvent *event)
{
    event->ignore();
}

void QCMultiMenu::setQCGeometry(int x, int y, int w, int h)
{
    if (w == -1){
        w = m_bHorizon?m_nMenuWidth*m_vMenus.size():m_nMenuWidth;
    }

    if (h == -1){
        h = m_bHorizon?m_nMenuHeight:m_nMenuHeight*m_vMenus.size();
    }

    this->setGeometry(x,y,w,h);
}

void QCMultiMenu::setHorizon(bool b)
{
    m_bHorizon = b;
}

void QCMultiMenu::setShowBorder(bool b)
{
    m_bShowBorder = b;
}

void QCMultiMenu::setMenuSize(int w, int h)
{
    m_nMenuWidth = w;
    m_nMenuHeight = h;
}

void QCMultiMenu::addMenu(const QString &iconPath)
{
    QCLabel* pLabel = new QCLabel(this);
    QPixmap iconSrc = QPixmap(iconPath);
	QPixmap icon = iconSrc.scaled(m_nMenuWidth, m_nMenuHeight, Qt::AspectRatioMode::KeepAspectRatio, Qt::TransformationMode::SmoothTransformation);
    pLabel->setPixmap(icon);
    pLabel->setAlignment(Qt::AlignCenter);

    int widthOffset = 0;
    int heightOffset = 0;
    if (m_bHorizon){
		widthOffset += m_vMenus.size() * (m_nMenuWidth - 1);
    }else{
		heightOffset += m_vMenus.size() * (m_nMenuHeight - 1);
    }

    pLabel->setGeometry(widthOffset,heightOffset,m_nMenuWidth,m_nMenuHeight);
    pLabel->show();


    if (m_bShowBorder){
        pLabel->setFrameShape (QFrame::Box);
        // if only one menu,set 4 radius;else,set first menu 2 radius and last menu 2 radius.
        if (m_bHorizon){
            if (m_vMenus.size() == 0){
				pLabel->setStyleSheet("border-width:1px;border-style:solid;border-color:#AAAAAA;border-radius:5px;");
            }else{
                pLabel->setStyleSheet("border-width:1px;border-style:solid;border-color:#AAAAAA;border-top-right-radius:5px;border-bottom-right-radius:5px;");
                QLabel* pFirst = m_vMenus[0];
                pFirst->setStyleSheet("border-width:1px;border-style:solid;border-color:#AAAAAA;border-top-left-radius:5px;border-bottom-left-radius:5px;");
                for (int n = 1 ; n < m_vMenus.size() ; ++n){
                    QLabel* pMid = m_vMenus[n];
                    pMid->setStyleSheet("border-width:1px;border-style:solid;border-color:#AAAAAA;border-radius:0px;");
                }
            }

        }else{
            if (m_vMenus.size() == 0){
                pLabel->setStyleSheet("border-width:1px;border-style:solid;border-color:#AAAAAA;border-radius:5px;");
            }else{
                pLabel->setStyleSheet("border-width:1px;border-style:solid;border-color:#AAAAAA;border-bottom-left-radius:5px;border-bottom-right-radius:5px;");
                QLabel* pFirst = m_vMenus[0];
                pFirst->setStyleSheet("border-width:1px;border-style:solid;border-color:#AAAAAA;border-top-left-radius:5px;border-top-right-radius:5px;");
                for (int n = 1 ; n < m_vMenus.size() ; ++n){
                    QLabel* pMid = m_vMenus[n];
                    pMid->setStyleSheet("border-width:1px;border-style:solid;border-color:#AAAAAA;border-radius:0px;");
                }
            }
        }
    }

    m_vMenus.push_back(pLabel);
}

QCLabel* QCMultiMenu::getAt(int n)
{
    if (n > m_vMenus.size())
        return NULL;

    return m_vMenus[n];
}

void QCMultiMenu::switchMenuIcon(int n, const QString& path)
{

}

void QCMultiMenu::selectMenu(int n)
{
	QCLabel* pLabel = m_vMenus[n];
	pLabel->setStyleSheet("background:#E0E0FF;border-width:1px;border-style:solid;border-color:#AAAAAA;border-radius:5px;");
}

void QCMultiMenu::deSelectMenu(int n)
{
	QCLabel* pLabel = m_vMenus[n];
	pLabel->setStyleSheet("background:#FFFFFF;border-width:1px;border-style:solid;border-color:#AAAAAA;border-radius:5px;");
}
